/**
 * 版权所有：公众信息
 * 项目名称:calendar
 * 创建者: dozen.zhang
 * 创建日期: 2016年3月13日
 * 文件说明: 
 */
package com.dozenx.util;



import cola.machine.game.myblocks.engine.paths.PathManager;
import core.log.LogUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
    public static List<File> readAllFileInFold(String path) throws IOException {
        File file = PathManager.getInstance().getHomePath().resolve(path).toFile();
        if(!file.exists()){
            LogUtil.println("not exsits "+path);
            System.exit(0);
        }
        if(!file.isDirectory()){
            LogUtil.err("it's not folder "+path);
        }
        return listFile(file);


    }

    public static List<File> listFile(File file ){
        List<File > fileList =new ArrayList<>();
        File[] fileAry = file.listFiles();
        for(File childFile : fileAry){
            if(childFile.isDirectory()){
                fileList.addAll(listFile(childFile));
            }else{
                fileList.add(childFile);
            }
        }
        return fileList;
    }
    public static String readFile2Str(String path) throws IOException {
        File file = PathManager.getInstance().getHomePath().resolve(path).toFile();
       return  readFile2Str(file);
    }
    public static String readFile2Str(File file) throws IOException {
        if (!file.exists()) {
            LogUtil.println("not exsits " + file);
            System.exit(0);
        }

        try ( InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
              BufferedReader br = new BufferedReader(isr)){
            String s;
            StringBuffer templateStr = new StringBuffer();
            while ((s = br.readLine()) != null) {
                s = s.trim();
                if (s.startsWith("//")) {
                    continue;
                }
                templateStr.append(s + "\r\n");
            }
            if (templateStr == null || templateStr.toString().length() == 0) {
                LogUtil.err("file is empty: " + file);
                // System.exit(0);
            }
            return templateStr.toString();
        }catch (Exception e){
            //e.printStackTrace();
            LogUtil.print(e);
            throw e;
        }

//        LogUtil.println(templateStr.toString());

    }
    public static void writeFile(File file ,String content) throws IOException {

        try (FileWriter fileWritter = new FileWriter(file, false);
             BufferedWriter  bufferWritter = new BufferedWriter(fileWritter)){

            //if file doesnt exists, then create it
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }

            //true = append file

            bufferWritter.write(content);


           // System.out.println("Done");

        } catch (IOException e) {
          LogUtil.print(e);
            throw e;
        }
    }
    public static void main3(String args[]){

    String s ="//123123";
    if(s.startsWith("//")){
        LogUtil.println("hello");
    }

    }

    public static List<String > readFile2List(String path) throws IOException {
        File file = PathManager.getInstance().getHomePath().resolve(path).toFile();
        if(!file.exists()){
            LogUtil.println("read file failed path:"+path);
        }
        LogUtil.println("read file from1 path:"+path);
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
      //  BufferedReader br = new BufferedReader(new FileReader(file));
        List<String> lines =new ArrayList();
        String s ;
        // StringBuffer templateStr = new StringBuffer();
        while ((s = br.readLine()) != null) {
            lines.add(s);
            // templateStr.append(s + "\r\n");
        }
        return lines;
    }

    public static List<String > readFile2List(File file ) throws IOException {

        if(!file.exists()){
            LogUtil.println("read file failed path:"+file.getPath());
        }
        LogUtil.println("read file from1 path:"+file.getPath());
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        //  BufferedReader br = new BufferedReader(new FileReader(file));
        List<String> lines =new ArrayList();
        String s ;
        // StringBuffer templateStr = new StringBuffer();
        while ((s = br.readLine()) != null) {
            lines.add(s);
            // templateStr.append(s + "\r\n");
        }
        return lines;
    }

    public static void main(String argsp[]){

        for(int i=0;i<30;i++){
            System.out.println(""+i+"    "+(i%10));
            if(i % 10 == 0){
                System.out.println("yes");
            }
        }
    }


    public static void main(){

    }
}
